package com.example.bookstore.web;

import com.example.bookstore.pojo.User;
import com.example.bookstore.service.UserService;
import com.example.bookstore.service.impl.UserServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;

//目前不使用————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        try {
            User login = userService.login(new User(username, password, null));
            if(login == null){
                //回显表单项信息
                req.setAttribute("msg","用户名或密码错误");
                req.setAttribute("username",username);

                req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
                System.out.println("用户" + username + "登录失败");


            }else{
                req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req,resp);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
